﻿using EntityModel;
using Microsoft.AspNetCore.Mvc;

namespace EGS.Controllers
{
    /// <summary>
    /// 假条
    /// </summary>
    [Route("apilll/[controller]/[action]")]
    [ApiController]
    public class ExcuseController : ControllerBase
    {
        private readonly EGSDbContext db;
        public ExcuseController(EGSDbContext context)
        {
            db = context;
        }

        public List<EGS_Excuse> All
        {
            get
            {
                string? name = "";
                name = null;
                if (string.IsNullOrEmpty(name))
                {
                    System.Console.WriteLine("name 为空");
                }

                //默认值是0


                //布尔类型是特殊的int 类型，只有0/1两个值，遵守int类型的计算规则
                //张三




                //select * from car
                return db.Excuses.Where(s => s != null).OrderByDescending(s => s.ID).ToList();
            }
        }




        //[HttpGet]
        //public List<ExcuseController> GetAll()
        //{
        //    return db.ExcuseController.Where(s => s.DelTime != null).OrderByDescending(sbyte=> sbyte.Id).ToList();
        //}

        //[HttpGet("GetAll")]
        //public List<ExcuseController> GetAll([FromBody] string Model, [FromBody] decimal Price, [FromBody] string Color, [FromBody])
        //{
        //    IQueryable<ExcuseController> query = db.ExcuseController;

        //    if (!string.IsNullOrEmpty(Model))
        //    {
        //        query = query.Where(s => s.Model == Model);
        //    }
        //    if (!string.IsNullOrEmpty(Color))
        //    {
        //        query = query.Where(s => s.color == color);
        //    }
        //    return query.ToList();
        //}



        public List<EGS_LoginInfo> GetAll()
        {
            return db.LoginInfo.Where(l => l.DelTime != null).OrderByDescending(l => l.ID).ToList();
        }


        [HttpGet]
        public List<EGS_LoginInfo> GetAll([FromQuery] string Ip, string Name)
        {
            IQueryable<EGS_LoginInfo> query = db.LoginInfo;
            if (!string.IsNullOrEmpty(Ip))
            {
                query = query.Where(l => l.IP == Ip);
            }
            if (!string.IsNullOrEmpty(Name))
            {
                query = query.Where(l => l.Name == Name);
            }
            return query.ToList();
            //IQueryable<EGS_LoginInfo> loginInfo = db.LoginInfo;
            //if (!string.IsNullOrEmpty(Ip))
            //{
            //    loginInfo = loginInfo.Where(l => l.LoginInfo == loginInfo);
            //}
            //return loginInfo.ToList();
        }
     

        /// <summary>
        /// 查
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("{id}")]
        public EGS_Excuse? GetEGS_Excuse(Guid id)
        {
            EGS_Excuse? login = new EGS_Excuse();
            login = db.Excuses.Where(l => l.ID == id).FirstOrDefault();
            return login;
        }
        /// <summary>
        /// 模糊查询名字
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        [HttpGet("like/{name}")]
        public List<EGS_Excuse> GetEGS_Excuses(string name)
        {
            List<EGS_Excuse> login = new List<EGS_Excuse>();
            
            return login;
        }

        /// <summary>
        /// 增
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        //[HttpPost]
        //public Result AddLogin([FromBody] EGS_Excuse login)
        //{
        //    Result result = new Result();
        //    db.LoginInfo.Add();
        //    result.Data = db.SaveChanges();
        //    return result;
        //}
        /// <summary>
        /// 删
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        [HttpDelete]
        public Result DelLogin(Guid Id)
        {
            Result? result = new Result();
            EGS_LoginInfo? loginInfo = new EGS_LoginInfo();
            loginInfo = db.LoginInfo.Where(w => w.ID == Id).FirstOrDefault();
            //int result = 0;
            if (loginInfo != null)
            {
                loginInfo.DelTime = DateTime.Now;
                db.LoginInfo.Update(loginInfo);
                result.Data = db.SaveChanges();
                //result.Code = 200;
                //result.Time = DateTime.Now;
                //result.Name = "张三";
            }
            return result;
        }
    }
    }

